package access;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Scanner;
import java.util.Vector;

import constants.General_Constants;

public class Vault {
	final public static String RosterDir = "\\data\\roster\\";
	final public static String KeyringDir = "\\data\\keys\\";
	
	public String CurRosterDir = null;
	public String CurKeyringDir = null;
	
	Vector<User> AllUsers = new Vector<User>();

	User currentUser = null;
	User HoldInfo	= new User();
	
	public Vault() {
		AllUsers.clear();
		loadUsers(Access_Level.ADMIN);
	}
	
	public void loadUsers(Access_Level a_Level) { 
		switch(a_Level) {
		case ADMIN:
			CurRosterDir = General_Constants.currentDir + RosterDir + "admin.roster";
			CurKeyringDir = General_Constants.currentDir + KeyringDir + "admin.keyring";
			break;
		case USER:
			break;
		case GUEST:
			break;
		default: break;
		}
		
		FileInputStream Rin = null;
		FileInputStream Kin = null;
		try {
			Rin = new FileInputStream(CurRosterDir);
			Kin = new FileInputStream(CurKeyringDir);
		} catch (FileNotFoundException e) { e.printStackTrace(); }
		
		Scanner ScanR = new Scanner(Rin,"UTF-8");
		Scanner ScanK = new Scanner(Kin,"UTF-8");
		ScanR.useDelimiter("▬");
		ScanK.useDelimiter("▬");
	    while(ScanR.hasNext()) {
	    	AllUsers.add(new User(ScanR.next(),ScanR.next(),Kin,ScanK));
	    }
	    
		ScanR.close();
		ScanK.close();
		
	}
	
	/**
	 * saveUsers
	 * @param a_Level
	 * @return
	 * <pre>If this function returns true, the file was saved successfully. If the Access_Level is not high enough,
	 * the user is not allowed to save User Files and the function returns false.</pre>
	 */
	public boolean saveUsers(Access_Level a_Level) {
		switch(a_Level) {
		case ADMIN:
		case USER:
			Writer outR = null;
			Writer outK = null;
			try {
				outR = new OutputStreamWriter(new FileOutputStream(new File(CurRosterDir)));
			
				outK = new OutputStreamWriter(new FileOutputStream(new File(CurKeyringDir)));
				User T = null;
				for(int i = 0;i < AllUsers.size();i++) {
					T = AllUsers.get(i);
					outR.write(T.getUser());		outR.write('▬');
					outR.write("" + T.getAccessLevel()); outR.write('▬');
					
					outK.write(T.getUser());		outK.write('▬');
					outK.write(T.getPass());		outK.write('▬');
				}
			}
			catch (FileNotFoundException e) { e.printStackTrace(); }
			catch (IOException e) { e.printStackTrace(); }
			finally {
				try {
					outR.close();
					outK.close();
				}
				catch (IOException e) { e.printStackTrace(); }
				
			}
			return true;
		case GUEST: return false;
		default: return false;
		}
	}

	public Access_Level getAccessLevel() {
		return currentUser.getAccessLevel();
	}
	
	public boolean isLoggedin() {
		if(currentUser == null) {
			return false;
		}
		return true;
	}
	
	public boolean login() {
		return login(HoldInfo.getUser(),HoldInfo.getPass());
	}
	
	public boolean login(String User,String Pass) {
		User T = null;
		for(int i = 0;i < AllUsers.size();i++) {
			T = AllUsers.get(i);
			if(T.getUser().equals(User))
				if(T.getPass().equals(Pass)) {
					currentUser = T;
					return true;	
				}
		}
		return false;
	}
	
	public void logout() {
		currentUser = null;
		HoldInfo.clear();
	}

	public boolean UserExists(String User) {
		User T = null;
		for(int i = 0;i < AllUsers.size();i++) {
			T = AllUsers.get(i);
			if(T.getUser().equals(User))
				return true;
		}
		return false;
	}

	
	public void holdUsername(String a_Entry) {
		HoldInfo.setUsername(a_Entry);
	}

	public void holdPassword(String a_Entry) {
		HoldInfo.setPassword(a_Entry);
	}
}
